package com.example.recite_words.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.example.recite_words.MainActivity;
import com.example.recite_words.R;
import com.google.android.material.snackbar.Snackbar;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;

import java.lang.ref.WeakReference;
import java.net.URI;
import java.util.Date;

public class Match extends MainActivity {
    /*    private final Handler handler = new Handler(){
            @Override
            public void handleMessage(Message msg){
                super.handleMessage(msg);
                if(msg.what==0x101){
                    Toast.makeText(Match.this,"接收到服务端消息",Toast.LENGTH_SHORT).show();
                }
            }
        };*/
    MyHandler myHandler = new MyHandler(Match.this);
    private static class MyHandler extends Handler {

        //弱引用持有HandlerActivity , GC 回收时会被回收掉
        private WeakReference<Match> weakReference;

        public MyHandler(Match activity) {
            this.weakReference = new WeakReference(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            Match activity = weakReference.get();
            super.handleMessage(msg);
            if (null != activity) {
                //执行业务逻辑
                StringBuffer sb = new StringBuffer();
                sb.append("服务器返回数据：");
                sb.append(msg.obj.toString());
                sb.append("\n");
                Toast.makeText(activity,"接收到服务端消息",Toast.LENGTH_SHORT).show();
            }
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);

        TextView showMessage = findViewById(R.id.test_textview);
        Button test = findViewById(R.id.test_button);
        URI serverURI = URI.create("ws://139.196.179.217/battle/link");
        WebSocketClient webSocketClient = new WebSocketClient(serverURI) {

            @Override
            public void onOpen(ServerHandshake serverHandshake) {
                StringBuffer sb = new StringBuffer();
                sb.append("onOpen at time：");
                sb.append(new Date());
                sb.append("服务器状态：");
                sb.append(serverHandshake.getHttpStatusMessage());
                sb.append("\n");
                showMessage.setText(sb.toString());

            }

            @Override
            public void onMessage(String message) {
                Message handlerMessage = Message.obtain();
                handlerMessage.obj = message;
                handlerMessage.what=0x101;
                myHandler.handleMessage(handlerMessage);
                Log.e("JWebSocketClientService", "发送的消息：" + message);

            }
            @Override
            public void onClose(int code, String reason, boolean remote) {
                StringBuffer sb = new StringBuffer();
                sb.append("onClose at time：");
                sb.append(new Date());
                sb.append("\n");
                sb.append("onClose info:");
                sb.append(code);
                sb.append(reason);
                sb.append(remote);
                sb.append("\n");
                showMessage.setText(sb.toString());
            }

            @Override
            public void onError(Exception ex) {
                StringBuffer sb = new StringBuffer();
                sb.append("onError at time：");
                sb.append(new Date());
                sb.append("\n");
                sb.append(ex);
                sb.append("\n");
                showMessage.setText(sb.toString());
            }
        };
        //webSocketClient.connect();
        new Thread(new Runnable(){
            @Override
            public void run(){
                webSocketClient.connect();
            }
        }).start();
        test.setOnClickListener(
                new View.OnClickListener() {
                    //内部匿名类
                    @Override
                    public void onClick(View view) {

                        if(webSocketClient.isClosed() || webSocketClient.isClosing()){
                            Snackbar.make(view,"Client正在关闭",Snackbar.LENGTH_SHORT).show();
                            webSocketClient.connect();
                        }else{
                            if (webSocketClient != null && webSocketClient.isOpen()) {
                                webSocketClient.send("你好");
                            }
                            /*StringBuffer sb = new StringBuffer();
                            sb.append("客户端发送消息：");
                            sb.append(new Date());
                            sb.append("\n");
                            sb.append("苹果很好吃");
                            sb.append("\n");
                            showMessage.setText(sb.toString());*/
                        }
                    }
                });
    }
    @Override
    protected void onDestroy() {
        //移除所有回调及消息
        myHandler.removeCallbacksAndMessages(null);
        super.onDestroy();
    }
}
